# include <Document.hpp>

namespace FTSS
{
	
CHAR_P Document::GetTermRange(const UINT_32 & iStartPos, UINT_32 iEndPos, UINT_32 & iRangeLength) throw()
{
	if (iStartPos >= index_size / sizeof(UINT_32)) { iRangeLength = 0; return NULL; }
	if (iEndPos > index_size / sizeof(UINT_32))    { iEndPos = index_size / sizeof(UINT_32) - 1; }
	if (iStartPos == iEndPos) { iRangeLength = 0; return NULL; }

	// Get start part
	UINT_32 iStartPart = 0;
	while (iStartPart < parts_size / sizeof(DocumentPart) && parts[iStartPart].index_offset <= iStartPos) { ++iStartPart; }
	UINT_32 iStartDataOffset  = iStartPart > 0 ? parts[iStartPart - 1].data_offset : 0;
	UINT_32 iStartIndexOffset = iStartPart > 0 ? parts[iStartPart - 1].index_offset : 0;

	// Get end part
	UINT_32 iEndPart = 0;
	while (iEndPart < parts_size / sizeof(DocumentPart) && parts[iEndPart].index_offset <= iEndPos) { ++iEndPart; }
	UINT_32 iEndIndexOffset = iEndPart > 0 ? parts[iEndPart - 1].index_offset : 0;

	DocumentIndex * aIndex = GetDocumentIndex();

	iRangeLength = iEndIndexOffset - iStartIndexOffset - aIndex[iStartPos].GetLength() - aIndex[iStartPos].GetOffset() + aIndex[iEndPos].GetOffset();

	return DocumentData() + iStartDataOffset + aIndex[iStartPos].GetOffset() + aIndex[iStartPos].GetLength();
	}
	
}
